package sensorlogic;

import gui.TempretureSensorForm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: kate
 * Date: 23.05.2009
 * Time: 10:49:38
 * To change this template use File | Settings | File Templates.
 */
public class Logic extends Thread {

    private TempretureSensorForm jframe;
    private boolean isProcessing;
    int temp;
    int timer;

    public Logic(TempretureSensorForm jframe) {

        this.jframe = jframe;

    }

    public static int getTemp(int t) {
        Random random = new Random();
        if (random.nextInt(5) < 3)
            return t += 1;
        else
            return t -= 1;
    }

    public void run() {

        try {
            if (jframe.AutoRB) {

                while (true) {
                    if (isProcessing) {

                        timer = Integer.parseInt(jframe.timeTextField.getText());

                        Date dtn = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("dd:mm:yy hh:mm:ss");
                        String date = formatter.format(dtn);


                        jframe.tempSensor.setTemp(jframe.tempSensor.getTemp(), timer, true);
                        temp = jframe.tempSensor.getTemp();
                        jframe.tempSensor.send(Integer.toString(temp));

                        String newline = "\n Time:" + date + "   Temperature: " + Integer.toString(temp) + "  C";
                        jframe.logTextArea.append(newline);
                        jframe.repaint();

                        try {
                            Thread.sleep(timer * 1000);
                        }
                        catch (Exception ex) {
                            System.out.println("Exception: " + ex);
                        }
                    }
                }
            }
        } catch (Exception ex) {
        }
        ;

    }


    public void startProcessing() {
        isProcessing = true;
    }

    public void stopProcesing() {
        isProcessing = false;
    }

}
